//
// Created by tcj on 2022/2/23.
//

#include "sylar/sylar.h"
#include "lastlog.h"

sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

void run_in_fiber(){
    SYLAR_LOG_INFO(g_logger) <<"run_in_fiber begin";
    sylar::Fiber::YieldToHold();
    SYLAR_LOG_INFO(g_logger) <<"run_in_fiber end";
    sylar::Fiber::YieldToHold();
    SYLAR_LOG_INFO(g_logger) <<"run_in_fiber ending";
}

void test_fiber(){
    //    从主线程切换到子线程，然后再切回来
    SYLAR_LOG_INFO(g_logger) << "main begin -1";
    {
        sylar::Fiber::GetThis();
        SYLAR_LOG_INFO(g_logger) << "main begin";
        sylar::Fiber::ptr fiber(new sylar::Fiber(run_in_fiber));
        fiber->swapIn();
        SYLAR_LOG_INFO(g_logger) << "main after swapin";
        fiber->swapIn();
        SYLAR_LOG_INFO(g_logger) << "main after end";
        fiber->swapIn();
    }
    SYLAR_LOG_INFO(g_logger)<<"main after end2";
}



int main(){

//    sylar::Thread::SetName("main");
//
//    std::vector<sylar::Thread::ptr> thrs;
//    for(int i=0;i<3;++i){
//        thrs.push_back(sylar::Thread::ptr(
//                new sylar::Thread(&test_fiber,"name_"+std::to_string(i))));
//    }
//
//    for(auto i:thrs){
//        i->join();
//    }
    sylar::Fiber::GetThis();
    SYLAR_LOG_INFO(g_logger)<<"main begin";
    sylar::Fiber::ptr ll(new sylar::Fiber(run_in_fiber));
    ll->call();
    SYLAR_LOG_INFO(g_logger)<<"1 main after";
    ll->call();
    SYLAR_LOG_INFO(g_logger)<<"2 main after";
//    SYLAR_LOG_INFO(g_logger)<<"main end";
    return 0;




}
